package com.nanhai.buddhist.college.module.collegebase.controller.admin.classroomcategory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.classroomcategory.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classroomcategory.ClassroomCategoryDO;
import com.nanhai.buddhist.college.module.collegebase.convert.classroomcategory.ClassroomCategoryConvert;
import com.nanhai.buddhist.college.module.collegebase.service.classroomcategory.ClassroomCategoryService;

@Api(tags = "管理后台 - 教室分类")
@RestController
@RequestMapping("/collegebase/classroom-category")
@Validated
public class ClassroomCategoryController {

    @Resource
    private ClassroomCategoryService classroomCategoryService;

    @PostMapping("/create")
    @ApiOperation("创建教室分类")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-category:create')")
    public CommonResult<String> createClassroomCategory(@Valid @RequestBody ClassroomCategoryCreateReqVO createReqVO) {
        return success(classroomCategoryService.createClassroomCategory(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新教室分类")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-category:update')")
    public CommonResult<Boolean> updateClassroomCategory(@Valid @RequestBody ClassroomCategoryUpdateReqVO updateReqVO) {
        classroomCategoryService.updateClassroomCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除教室分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-category:delete')")
    public CommonResult<Boolean> deleteClassroomCategory(@RequestParam("id") String id) {
        classroomCategoryService.deleteClassroomCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得教室分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-category:query')")
    public CommonResult<ClassroomCategoryRespVO> getClassroomCategory(@RequestParam("id") String id) {
        ClassroomCategoryDO classroomCategory = classroomCategoryService.getClassroomCategory(id);
        return success(ClassroomCategoryConvert.INSTANCE.convert(classroomCategory));
    }

    @GetMapping("/list")
    @ApiOperation("获得教室分类列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-category:query')")
    public CommonResult<List<ClassroomCategoryRespVO>> getClassroomCategoryList(@RequestParam("ids") Collection<String> ids) {
        List<ClassroomCategoryDO> list = classroomCategoryService.getClassroomCategoryList(ids);
        return success(ClassroomCategoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得教室分类分页")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-category:query')")
    public CommonResult<PageResult<ClassroomCategoryRespVO>> getClassroomCategoryPage(@Valid ClassroomCategoryPageReqVO pageVO) {
        PageResult<ClassroomCategoryDO> pageResult = classroomCategoryService.getClassroomCategoryPage(pageVO);
        return success(ClassroomCategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出教室分类 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-category:export')")
    @OperateLog(type = EXPORT)
    public void exportClassroomCategoryExcel(@Valid ClassroomCategoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ClassroomCategoryDO> list = classroomCategoryService.getClassroomCategoryList(exportReqVO);
        // 导出 Excel
        List<ClassroomCategoryExcelVO> datas = ClassroomCategoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "教室分类.xls", "数据", ClassroomCategoryExcelVO.class, datas);
    }

    @GetMapping("/byName/list")
    @ApiOperation("获得教室分类列表--支持昵称模糊查询 ")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-category:query')")
    public CommonResult<List<ClassroomCategoryRespVO>> getClassroomCategoryListByStr(String name) {
        List<ClassroomCategoryDO> list = classroomCategoryService.getClassroomCategoryListByStr(name);
        return success(ClassroomCategoryConvert.INSTANCE.convertList(list));
    }


}
